#!/usr/bin/env sh

cd "$(dirname "$0")"
. "./.common.sh"
cd ../..

PATH="$PWD/sandbox/composer/bin:$PATH"

if ! chkcmd 'peridot'; then
        echo ' error: "peridot" command not found.'
        echo ' Execute "./support/tools/init" first.'
        exit 1
fi

coverage_index="$PWD/sandbox/code-coverage-report/index.html"

## It's is not created automatically.
mkdir -p "$(dirname "$coverage_index")"

reporter=html-code-coverage

if test $# -eq 1; then
        reporter=$1
fi

cd sandbox

peridot_arguments="-c \"../support/peridot.php\" -r $reporter -g \"*.php\" \"../specs/\""

if php -m | grep -i 'xdebug' > /dev/null; then
        echo ' info: using Xdebug.'
        eval "peridot $peridot_arguments"

elif chkcmd 'phpdbg'; then
        echo ' info: using phpdbg.'
        echo
        echo ' type: run'
        echo '       quit'
        echo

        eval "phpdbg -e \"$(which peridot)\" $peridot_arguments"
else
        echo ' error: no profiling tool found.'
        exit 1
fi

if test -f "$coverage_index" && chkcmd 'open'; then
        open "$coverage_index"
fi
